set nocompatible        " 取消vi兼容模式


" some new setting {{
    let mapleader=" "

    noremap Q :q<CR>
    noremap S :w<CR>

    noremap <LEADER>st :Startify<CR>

    " Search
    noremap <LEADER>nl :nohlsearch<CR>

    " H key: go to the start of the line
    noremap <silent> H ^
    " L key: go to the end of the line
    noremap <silent> L $

    " split the screens to up (horizontal), down (horizontal), left (vertical), right (vertical)
    noremap sk :set nosplitbelow<CR>:split<CR>:set splitbelow<CR>
    noremap sj :set splitbelow<CR>:split<CR>
    noremap sh :set nosplitright<CR>:vsplit<CR>:set splitright<CR>
    noremap sl :set splitright<CR>:vsplit<CR>

    " Resize splits with arrow keys
    noremap <c-up> :res +5<CR>
    noremap <c-down> :res -5<CR>
    noremap <c-left> :vertical resize-5<CR>
    noremap <c-right> :vertical resize+5<CR>

    " Tab management
    " Create a new tab with tu
    noremap te :tabe<CR>
    " Move around tabs with tn and ti
    noremap tn :-tabnext<CR>
    noremap tp :+tabnext<CR>
    " Move the tabs with tmn and tmi
    noremap tmn :-tabmove<CR>
    noremap tmp :+tabmove<CR>
" }}

" Vim setting {{
    " 复制粘贴 {
        " nvim 需下载 xclip or xsel
        set clipboard=unnamedplus
        "vnoremap <leader>y "+y
        "nnoremap <leader>p "+p
        "vnoremap <leader>x "+x
    " }
    
    " 关闭方向键，强制使用hjkl {
        "map <Left> <Nop>
        "map <Right> <Nop>
        "map <Up> <Nop>
        "map <Down> <Nop>
    " }
    
    " 通用设置 {
        filetype on
        filetype plugin on
        set noerrorbells	    " 不让vim发出讨厌的滴滴声
        set noeb 			    " 去掉输入错误的提示
        set report=0 		    " 告诉我们文件的哪一行被改变过
    " }

    " 编码设置 {
        set langmenu=zh_CN.UTF-8
        set helplang=cn
        set termencoding=utf-8
        set encoding=utf8
        set fileencodings=utf8,gbk,cp936,gb2312,gb18030 		" 使用utf-8或gbk打开文件
    " }

    " 显示设置 {
        syntax enable 		" 开启语法高亮功能
        syntax on 			" 允许指定语法高亮配色方案替换默认方案
        filetype indent on
        set number 
        set relativenumber
        set cursorline
        "set colorcolumn=80
        set t_Co=256
        set background=dark
        colorscheme gruvbox
        set nowrap 			" 禁止折行 
        set showcmd 
        set showmode  		" 命令行显示vim当前模式 
        set showmatch 
        set linespace=0 	" 字符间插入的像素行数目 
        set matchtime=1 	" 匹配括号高亮的时间（单位是十分之一秒）		
        set backspace=2 	" 使用回格键正常处理indent,eol,start等 
        set cmdheight=2 
        set laststatus=0    " 显示状态栏 
        set scrolloff=3 	" 光标移动到buffer的顶部和底部时保持3行的距离 
        set tabstop=4 		" 设置编辑时制表符占用空格数
        set softtabstop=4
        set shiftwidth=4
        set smartindent
        set expandtab 		" 将制表符扩展为空格
        set smarttab		" 在行和段开始处使用制表符
        set history=1000
        set fillchars=vert:\ ,stl:\ ,stlnc:\ 	" 在被分割的窗口间显示空白，便于阅读
        set iskeyword+=_,$,@,%,#,-				" 带有如下符号的单词不要被换行分割
        set whichwrap+=<,>,h,l 					" 允许backspace和光标键跨越行边界
        "set listchars=tab:\|\ ,trail:▫
        set listchars=tab:\|\ ,trail:-
    " }

    " 搜索设置 {
        set hlsearch 		" 高亮显示搜索结果
        set incsearch 		" 开启实时搜索功能
        set ignorecase 		" 搜索时大小写不敏感
        set magic 			" 设置魔术
    " }

    " 代码折叠 {
        set foldmethod=indent
        "set foldmethod=syntax
        set nofoldenable 	" 启动vim时关闭折叠代码
        "set foldenable
        "使用zc按键来创建折叠，使用za来打开或者关闭折叠,za经常会误输入，可以用空格键来替代za
        "nnoremap <space> za
    " }

    " 其它设置 {
        set wildmenu      " vim自身命令行模式智能补全
        set nobackup
        set nowritebackup
        set noswapfile
        set noundofile
        set autoread      " 文件在vim之外修改过，自动重新读入
        set autowrite     " 设置自动保存
        set confirm       " 在处理未保存或只读文件的时候，弹出确认
        set splitbelow
        "set splitright
        set ma
    " }

    " 分屏设置 {
        nnoremap <C-J> <C-W><C-J>
        nnoremap <C-K> <C-W><C-K>
        nnoremap <C-L> <C-W><C-L>
        nnoremap <C-H> <C-W><C-H>
    " }
" }}

if exists("g:neovide")
    " Put anything you want to happen only in Neovide here
    set guifont=FiraCode\ Nerd\ Font\ Mono:h12
    let g:neovide_transparency = 0.95
    let g:neovide_hide_mouse_when_typing = v:true
    let g:neovide_confirm_quit = v:true
    let g:neovide_fullscreen = v:false
    let g:neovide_remember_window_size = v:true
    let g:neovide_cursor_vfx_mode = "railgun"
endif

" vim-plug {{
    " Specify a directory for plugins 
    call plug#begin('~/.config/nvim/plugged')
    Plug 'mhinz/vim-startify'
    Plug 'scrooloose/nerdtree'    
    Plug 'scrooloose/nerdcommenter'
    Plug 'Yggdroot/indentLine'
    Plug 'jiangmiao/auto-pairs'
    Plug 'mattn/emmet-vim'
    Plug 'iamcco/dict.vim'
    Plug 'itchyny/vim-cursorword'
    Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
    Plug 'tbodt/deoplete-tabnine', {'do': './install.sh'}
    Plug 'Chiel92/vim-autoformat'
    Plug 'voldikss/vim-floaterm'


    " Initialize plugin system
    call plug#end()
" }}

" Plugins Setting {{
    " floaterm {
        let g:floaterm_keymap_new    = '<leader>nf'
        let g:floaterm_keymap_prev   = '<leader>fp'
        let g:floaterm_keymap_next   = '<leader>fn'
        let g:floaterm_keymap_toggle = '<leader>ft' 
    " }

    " startify_custom_header {
        " replace boxed to cowsay, to display the cow
        let g:startify_custom_header = 'startify#pad(startify#fortune#boxed())'
    " }

    " autoformat{
        au BufWrite *.py,*.html,*.js,*.css,*.json :Autoformat
    " }

    " emmet {
        " n-normal / inv-all fucntion ,which is equal to / a-all function in
        " all mode
        let g:user_emmet_mode='a'

        let g:user_emmet_install_global = 0
        autocmd FileType html,css EmmetInstall
        
        " default: (<c-y> + ,) 
        "let g:user_emmet_leader_key='<c-z>'
        let g:user_emmet_expandabbr_key='<Tab>'
         
        " }

    " nerdtree {
        let g:NERDTreeWinPos='left'
        let g:NERDTreeSize=30
        let g:NERDTreeShowLineNumbers=1
        let g:NERDTreeHidden=0
        map <leader>t :NERDTreeToggle<CR>

        " 打开vim时自动打开nerdtree, 但是不再显示欢迎界面---插件：starify
        " autocmd vimenter * NERDTree
    " }
" }}

" python format {
    au BufNewFile,BufRead *.py
    \ set tabstop=4 |
    \ set softtabstop=4 |
    \ set shiftwidth=4  |
    \ set textwidth=79  |
    \ set expandtab     |   "tab替换为空格键
    \ set autoindent    |
    \ set fileformat=unix   "保存文件格式"
" }

" 新建文件插入文件头 {
    autocmd BufNewFile *.sh,*.py,*.java exec ":call SetTitle()"
    func SetTitle()
        if expand("%:e") == 'sh'
            call setline(1,"#!/bin/bash")
            call append(line("."), "")
        elseif expand("%:e") == 'py'
            call setline(1, "#!/usr/bin/python3")
            call append(line("."), "# -*- coding:utf-8 -*-")
            call append(line(".")+1,"")
        elseif expand("%:e") == 'java'
            call setline(1, "")
            call append(line("."), "public class ".expand("%:r")." {")
            call append(line(".")+1, "    public static void main(String[] args) {")
            call append(line(".")+2, "")
            call append(line(".")+3, "    }")
            call append(line(".")+4, "}")
        endif
    endfunc
    autocmd BufNewFile * normal G
" }

" Python,Sh,c,cpp,java,html 编译、运行 {
    map <F5> :call CompileRun()<CR>
    func! CompileRun()
        exec "w"
        if &filetype == 'sh'
            :!time bash %
        elseif &filetype == 'python'
            set splitbelow
            :sp
            :term python %
        elseif &filetype == 'go'
            set splitbelow
            :sp
            :term go run %
        elseif &filetype == 'c'
            exec "!g++ % -o %<"
            exec "!time ./%<"
        elseif &filetype == 'cpp'
            set splitbelow
            exec "!g++ -std=c++11 % -Wall -o %<"
            :sp
            :res -15
            :term ./%<
            "exec "!g++ % -std=c++11 -o %<"
            "exec "!time ./%<"
        elseif &filetype == 'java'
            exec "!javac %"
            exec "!time java %<"
        elseif &filetype == 'html'
            silent! exec "!chromium % &"
        endif
    endfunc
" }

" c, c++的调试 {
    map <F6> :call Rungdb()<CR>
    func! Rungdb()
        exec "w"
        exec "!g++ % -std=c++11 -g -o %<"
        exec "!gdb ./%<"
    endfunc
" }

